package com.coffee.soft.controller;


import com.alibaba.fastjson.JSON;
import com.coffee.soft.entity.ItemsCarousel;
import com.coffee.soft.service.ItemsShowPageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
/**
 * @Author:LiuRuidong
 * @Description:  商品前台展示页面
 * 测试已成功
 * @Date: Created in 23:45 2018/2/5
 * @Modified By:
 */
//     使spring-mvc可以扫描并进行注解
@Controller
public class ItemsShowPageController {

    //    自动注入，控制前端页面和后台的数据交互
    @Autowired
    private ItemsShowPageService itemsShowPageService;
    /**
     * @Author：LiuRuidong
     * @Description: 轮播图加载页
     * @Date:2018/2/5 15:41
     * @Param:[]
     * @return java.util.List<com.coffee.soft.entity.ItemsCarousel>
     */
    @RequestMapping("/indexLoad")
    @ResponseBody
    public void indexLoadHandler(HttpServletRequest request, HttpServletResponse response) {
        String callback = request.getParameter("callback");
        List<ItemsCarousel> itemsCarouselsList=itemsShowPageService.CarouseItems();
        String json = JSON.toJSONString(itemsCarouselsList);
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();
            if ((callback!="")&&(callback!=null)){
                json=callback+"("+json+")";
            }
            pw.println(json);
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * @Author：LiuRuidong
     * @Description: 上新加载页
     * @Date:2018/2/5 15:46
     * @Param:
     * @return
     */
    @RequestMapping("/changeNewImage")
    @ResponseBody
    public void changeNewImageHandler(HttpServletRequest request, HttpServletResponse response) {
        String callback = request.getParameter("callback");
        List<ItemsCarousel> itemsOnTheNewList=itemsShowPageService.OnTheNewItems();
        String json = JSON.toJSONString(itemsOnTheNewList);
        try {
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();
            if ((callback!="")&&(callback!=null)){
                json=callback+"("+json+")";
            }
            pw.println(json);
            pw.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
